import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Http, Headers, RequestOptions} from '@angular/http';
import {DatePipe} from '@angular/common';
import {QuotaRank} from '../entity/quota-rank.model';
import {InterceptorService} from './interceptor/interceptor-service';
import {PowerDeviation} from '../entity/power-deviation.model';

@Injectable()
export class QuotaRankService {
  private fomat: string = 'yyyy-MM-dd';
  private readonly baseUrl = `${AppConfig.APP_RESTFUL}`;

  private readonly param_farm = 'rPowers/getCompanyRank';
  private readonly url_farm = `${this.baseUrl}/${this.param_farm}`;

  private readonly param_farmId = 'rPowers/getFarmRank';
  private readonly url_farmId = `${this.baseUrl}/${this.param_farmId}`;

  private readonly param_deviation = 'rPowers/getSysRPowerListByFarmIds';
  private readonly url_deviation = `${this.baseUrl}/${this.param_deviation}`;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe) {
  }

  /**
   * 获取所有风场的各指标排名
   *
   * @returns {Promise<PowerLoss[]>}
   * @memberof PowerLossRankService
   */
  getFarmQuotaRankList(farmId: number, quotaType: number, dateType: number, date: Date, rankType: number, rankValue: number): Promise<{ rankingDataFarmList: QuotaRank[], rankingDataTurbineList: QuotaRank[] }> {
    let dateFomat: string = '';
    dateFomat = this.datePipe.transform(date, this.fomat);
    return this.http.post(this.url_farm, JSON.stringify({
      'farmId': farmId,
      'paramType': quotaType,
      'dateType': dateType,
      'date': dateFomat,
      'rankType': rankType,
      'rankValue': rankValue
    }))
      .toPromise()
      .then(response => {
        let resault = response;
        try {
          switch (resault.code) {
            case '200':
              return resault.data;
            case '-1':
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  /**
   * 获取所有风机的各指标排名数据
   *
   * @returns {Promise<PowerLoss[]>}
   * @memberof PowerLossRankService
   */
  getTurbQuotaRankList(farmId: number, quotaType: number, dateType: number, date: Date, rankType: number, rankValue: number): Promise<{ farmQuota: QuotaRank[], turbList: QuotaRank[] }> {

    let dateFomat: string = '';
    dateFomat = this.datePipe.transform(date, this.fomat);

    return this.http.post(this.url_farm, JSON.stringify({
      'farmId': farmId,
      'paramType': quotaType,
      'dateType': dateType,
      'date': dateFomat,
      'rankType': rankType,
      'rankValue': rankValue
    }))
      .toPromise()
      .then(response => {
        let resault = response;
        try {
          switch (resault.code) {
            case '200':
              return resault.data;
            case '-1':
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  /**
   * 获取当前风场下的风机指标排名集合
   *
   * @returns {Promise<PowerLoss[]>}
   * @memberof PowerLossRankService
   */
  getTurbQuotaRankListByFarmId(farmId: number, quotaType: number, dateType: number, date: Date, rankType: number, rankValue: number): Promise<QuotaRank[]> {

    let dateFomat: string = '';
    dateFomat = this.datePipe.transform(date, this.fomat);

    return this.http.post(this.url_farmId, JSON.stringify({
      'farmId': farmId,
      'paramType': quotaType,
      'dateType': dateType,
      'date': dateFomat,
      'rankType': rankType,
      'rankValue': rankValue
    }))
      .toPromise()
      .then(response => {
        let resault = response;
        try {
          switch (resault.code) {
            case '200':
              return resault.data as QuotaRank[];
            case '-1':
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  getPowersDeviation(ids: number[], startTime: Date, endTime: Date, dateType: number, powerType: number[]): Promise<PowerDeviation[]> {
    let startFomat: string = '';
    let endFomat: string = '';
    startFomat = this.datePipe.transform(startTime, this.fomat);
    endFomat = this.datePipe.transform(endTime, this.fomat);
    return this.http.post(this.url_deviation, JSON.stringify({
      'farmIds': ids,
      'startTime': startFomat,
      'endTime': endFomat,
      'dateType': dateType,
      'powerTypes': powerType
    }))
      .toPromise()
      .then(response => {
        let resault = response;
        try {
          switch (resault.code) {
            case '200':
              return resault.data as PowerDeviation[];
            case '-1':
              throw new Error(resault.message);
          }
        }
        catch (error) {
          throw error;
        }
      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }
}


// WEBPACK FOOTER //
// ./src/app/service/quota-rank.service.ts
